package com.zxjbyte.yiyi.framework.web.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

/**
 * @Author zhangxingjia
 * @Date 2023/2/28 15:51
 * @Version: 1.0
 */
@Data
@ConfigurationProperties(SwaggerProperties.SWAGGER_PROP_PREFIX)
@Configuration
public class SwaggerProperties {

    public static final String SWAGGER_PROP_PREFIX = "springdoc.info";

    private String title;
    private String description;
    private String termsOfService = ""; // 服务条款的 URL，用来明确 API 的使用规则
    private ApiContact contact = new ApiContact();
    private ApiLicense license = new ApiLicense();


    @Data
    public static class ApiContact {

        private String name;
        private String url;
        private String email;
    }

    @Data
    public static class ApiLicense {

        private String name;
        private String url;
    }

    @Bean
    public OpenAPI openAPI(@Value("${info.app.version}") String version) {
        return new OpenAPI()
//                .components(new Components().addSecuritySchemes("basicScheme", new SecurityScheme().type(SecurityScheme.Type.APIKEY).scheme(HttpHeaders.AUTHORIZATION).in(SecurityScheme.In.HEADER))
//                        .addParameters("myHeader1", new Parameter().in("header").schema(new StringSchema()).name("myHeader1")).addHeaders("myHeader2", new Header().description("myHeader2 header").schema(new StringSchema())))
                .info(new Info()
                        .title(title)
                        .version(version)
                        .description(description)
                        .termsOfService(termsOfService)
                        .contact(new Contact().name(contact.name).url(contact.url).url(contact.url))
                        .license(new License().name(license.name).url(license.url)));
    }
}
